package com.jinzhi.jzweb.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.jinzhi.common.base.BaseController;
import com.jinzhi.common.utils.Result;
import com.jinzhi.jzweb.domain.ExaminationResultsComDO;
import com.jinzhi.jzweb.domain.StuSignUpComDO;
import com.jinzhi.jzweb.pojo.ExaminationAnswerSheetComOneExlVO;
import com.jinzhi.jzweb.pojo.VtatisticsVo;
import com.jinzhi.jzweb.service.ExaminationResultsComService;
import com.jinzhi.jzweb.service.StuSignUpComService;
import com.jinzhi.jzweb.strategy.factory.SourConfig;
import com.jinzhi.jzweb.utils.EasyPoiUtil;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 超级管理员 总裁篇查看分数
 * <pre>
 * B赛场查看分数 统计
 * 2023
 * </pre>
 * <small> 2023-05-05 15:11:09 | lance</small>
 */
@Controller
@RequestMapping("/jzweb/examinationAnswerSheetComOneSour")
public class ExaminationAnswerSheetComOneSourController extends BaseController {
    @Autowired
    private StuSignUpComService stuSignUpService;
    @Autowired
    private ExaminationResultsComService examinationResultsService;


    /**
     * C赛场成绩统计导出
     *
     * @param response
     */
    @GetMapping("/downloadImportStu")
    @RequiresPermissions("jzweb:stuSignUpCom:import")
    @ResponseBody
    public void downloadImportTemplate(HttpServletResponse response, ExaminationResultsComDO examinationAnswerSheetComDT) {
        List<ExaminationAnswerSheetComOneExlVO> list1 = new LinkedList<>();
        Wrapper<StuSignUpComDO> wrapper=new EntityWrapper<>();

        if(StringUtils.isNotBlank(examinationAnswerSheetComDT.getUnitName())){
            wrapper.like("unit_name",examinationAnswerSheetComDT.getUnitName());
        }
        // 查询列表数据
        Page<StuSignUpComDO> list = stuSignUpService.selectPage(getPage(StuSignUpComDO.class),
                wrapper.eq("decrypt",1).orderBy("id", false));
        for(StuSignUpComDO stuSignUpComDO:list.getRecords()){
            ExaminationResultsComDO examinationResultsComDO=  examinationResultsService.findOneByKv("stu_sign_id",stuSignUpComDO.getId());
            ExaminationAnswerSheetComOneExlVO exlVO=new ExaminationAnswerSheetComOneExlVO();
            if(examinationResultsComDO!=null){
                ExaminationResultsComDO obj = examinationResultsComDO;
                exlVO.setComNums(stuSignUpComDO.getComNums());
                exlVO.setName(stuSignUpComDO.getName());
                exlVO.setUnitName(stuSignUpComDO.getUnitName());
                exlVO.setIdNumber(stuSignUpComDO.getIdNumber());
                if(obj.getScoingb1()==null){
                    obj.setScoingb1(0.0);
                }
                if(obj.getScoingb2()==null){
                    obj.setScoingb2(0.0);
                }
                if(obj.getScoingb3()==null){
                    obj.setScoingb3(0.0);
                }
                if(obj.getScoingb4()==null){
                    obj.setScoingb4(0.0);
                }
                if(obj.getScoingb5()==null){
                    obj.setScoingb5(0.0);
                }
                //a2
                if(obj.getScoingb1s()==null){
                    obj.setScoingb1s(0.0);
                }
                if(obj.getScoingb2s()==null){
                    obj.setScoingb2s(0.0);
                }
                if(obj.getScoingb3s()==null){
                    obj.setScoingb3s(0.0);
                }
                if(obj.getScoingb4s()==null){
                    obj.setScoingb4s(0.0);
                }
                if(obj.getScoingb5s()==null){
                    obj.setScoingb5s(0.0);
                }

                //c1
                if(obj.getScoingc1()==null){
                    obj.setScoingc1(0.0);
                }
                if(obj.getScoingc2()==null){
                    obj.setScoingc2(0.0);
                }
                if(obj.getScoingc3()==null){
                    obj.setScoingc3(0.0);
                }
                if(obj.getScoingc4()==null){
                    obj.setScoingc4(0.0);
                }
                if(obj.getScoingc5()==null){
                    obj.setScoingc5(0.0);
                }
                //c2
                if(obj.getScoingc2s()==null){
                    obj.setScoingc2s(0.0);
                }
                if(obj.getScoingc1s()==null){
                    obj.setScoingc1s(0.0);
                }
                if(obj.getScoingc3s()==null){
                    obj.setScoingc3s(0.0);
                }
                if(obj.getScoingc4s()==null){
                    obj.setScoingc4s(0.0);
                }
                if(obj.getScoingc5s()==null){
                    obj.setScoingc5s(0.0);
                }
                //c3
                if(obj.getScoingc1ls()==null){
                    obj.setScoingc1ls(0.0);
                }
                if(obj.getScoingc2ls()==null){
                    obj.setScoingc2ls(0.0);
                }
                if(obj.getScoingc3ls()==null){
                    obj.setScoingc3ls(0.0);
                }
                if(obj.getScoingc4ls()==null){
                    obj.setScoingc4ls(0.0);
                }
                if(obj.getScoingc5ls()==null){
                    obj.setScoingc5ls(0.0);
                }
                //A
                if(obj.getScore1()==null){
                    obj.setScore1(0.0);
                }
                if(obj.getScore2()==null){
                    obj.setScore2(0.0);
                }
                if(obj.getScore3()==null){
                    obj.setScore3(0.0);
                }
                if(obj.getScore4()==null){
                    obj.setScore4(0.0);
                }
                if(obj.getScore5()==null){
                    obj.setScore5(0.0);
                }

                if(obj.getScore1s()==null){
                    obj.setScore1s(0.0);
                }
                if(obj.getScore2s()==null){
                    obj.setScore2s(0.0);
                }
                if(obj.getScore3s()==null){
                    obj.setScore3s(0.0);
                }
                if(obj.getScore4s()==null){
                    obj.setScore4s(0.0);
                }
                if(obj.getScore5s()==null){
                    obj.setScore5s(0.0);
                }


                //B赛场个人统计
                double jg = obj.getScore1() + obj.getScore2() +obj.getScore3();
                jg = (jg / 3);
                BigDecimal bg = new BigDecimal(jg);
                double f1 = bg.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
                exlVO.setScore2(String.valueOf(f1));

                //A赛场个人统计
                double b=(obj.getScoingb1() + obj.getScoingb2() + obj.getScoingb3());
                b = (b / 3);
                b = b + (obj.getTotalScore() * 0.3);
                BigDecimal bp = new BigDecimal(b);
                double bs = bp.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
                exlVO.setScore1(String.valueOf(bs));

                //C赛场个人统计
                Double c=(obj.getScoingc1() + obj.getScoingc2() + obj.getScoingc3());
                c = (c / 3);
                BigDecimal cp = new BigDecimal(c);
                double cs = cp.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
                exlVO.setScore3(String.valueOf(cs));

                Double tatlo=(cs+bs+f1);
                BigDecimal bgs = new BigDecimal(tatlo);
                double tatlos = bgs.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
                exlVO.setScore(tatlos);
                list1.add(exlVO);
            }else {
                ExaminationAnswerSheetComOneExlVO obj=new ExaminationAnswerSheetComOneExlVO();
                obj.setComNums(stuSignUpComDO.getComNums());
                obj.setName(stuSignUpComDO.getName());
                obj.setUnitName(stuSignUpComDO.getUnitName());
                obj.setIdNumber(stuSignUpComDO.getIdNumber());
                obj.setScore1("0.0");
                obj.setScore2("0.0");
                obj.setScore3("0.0");
                obj.setScore(0.0);
                list1.add(obj);

            }
        }
        //升序排列
        list1 = list1.stream().sorted(Comparator.comparing(ExaminationAnswerSheetComOneExlVO::getScore).reversed()).collect(Collectors.toList());
        //导出操作
        EasyPoiUtil.exportExcel(forDes(list1), "赛场个人统计成绩导出", "赛场个人统计成绩导出", ExaminationAnswerSheetComOneExlVO.class, "赛场个人统计成绩导出", response);
    }

        public List<ExaminationAnswerSheetComOneExlVO> forDes(List<ExaminationAnswerSheetComOneExlVO> list1){
            if(list1.size()==0){
                return new ArrayList<>();
            }
            for (int i = 0; i < list1.size(); i++) {
                list1.get(i).setDes(i+1);
            }
            return list1;
        }

    /**
     * 超级管理员 一级管理员查看分数
     * @param model
     * @return
     */
    @GetMapping()
    @RequiresPermissions("jzweb:examinationAnswerSheetCom:examinationAnswerSheetComSour")
    String info( Model model) {
        return "jzweb/examinationAnswerSheetCom/examinationAnswerSheetComOneSour";
    }


    /**
     * B赛场理论成绩统计
     * @param examinationAnswerSheetComDTO
     * @return
     */
    @ResponseBody
    @GetMapping("/list")
    @RequiresPermissions("jzweb:examinationAnswerSheetCom:examinationAnswerSheetComSour")
    public Result<VtatisticsVo> list(ExaminationResultsComDO examinationAnswerSheetComDTO){
        VtatisticsVo vtatisticsVo = new VtatisticsVo();
        Wrapper<StuSignUpComDO> wrapper=new EntityWrapper<>();

        if(StringUtils.isNotBlank(examinationAnswerSheetComDTO.getUnitName())){
            wrapper.like("unit_name",examinationAnswerSheetComDTO.getUnitName());
        }

        // 查询列表数据
        Page<StuSignUpComDO> page = stuSignUpService.selectPage(getPage(StuSignUpComDO.class),
                wrapper.orderBy("id", false));

        double tatlos = 0.0; //个人总分
        double max = 0.0; //最高分
        double min = 100.0; //最低分
        List list = new ArrayList();
        for (int i = 0; i < page.getRecords().size(); i++) {
            ExaminationResultsComDO examinationResultsComDO=  examinationResultsService.findOneByKv("stu_sign_id",page.getRecords().get(i).getId());
            StuSignUpComDO stuSignUpComDO=page.getRecords().get(i);
            if(examinationResultsComDO!=null){
                ExaminationResultsComDO obj = examinationResultsComDO;
                obj.setNum2(stuSignUpComDO.getNum2());
                obj.setName(stuSignUpComDO.getName());
                obj.setUnitName(stuSignUpComDO.getUnitName());
                obj.setIdNumber(stuSignUpComDO.getIdNumber());
                obj.setTicketNumber(stuSignUpComDO.getTicketNumber());
                obj.setComNums(stuSignUpComDO.getComNums());
                obj.setDecrypt(stuSignUpComDO.getDecrypt());
                if(obj.getTotalScore()==null){
                    obj.setTotalScore(0.0);
                }
                Double sour=obj.getTotalScore();
            //a1
                if(obj.getScoingb1()==null){
                    obj.setScoingb1(0.0);
                }
                if(obj.getScoingb2()==null){
                    obj.setScoingb2(0.0);
                }
                if(obj.getScoingb3()==null){
                    obj.setScoingb3(0.0);
                }
                if(obj.getScoingb4()==null){
                    obj.setScoingb4(0.0);
                }
                if(obj.getScoingb5()==null){
                    obj.setScoingb5(0.0);
                }
            //a2
                if(obj.getScoingb1s()==null){
                    obj.setScoingb1s(0.0);
                }
                if(obj.getScoingb2s()==null){
                    obj.setScoingb2s(0.0);
                }
                if(obj.getScoingb3s()==null){
                    obj.setScoingb3s(0.0);
                }
                if(obj.getScoingb4s()==null){
                    obj.setScoingb4s(0.0);
                }
                if(obj.getScoingb5s()==null){
                    obj.setScoingb5s(0.0);
                }

            //c1
                if(obj.getScoingc1()==null){
                    obj.setScoingc1(0.0);
                }
                if(obj.getScoingc2()==null){
                    obj.setScoingc2(0.0);
                }
                if(obj.getScoingc3()==null){
                    obj.setScoingc3(0.0);
                }
                if(obj.getScoingc4()==null){
                    obj.setScoingc4(0.0);
                }
                if(obj.getScoingc5()==null){
                    obj.setScoingc5(0.0);
                }
            //c2
                if(obj.getScoingc2s()==null){
                    obj.setScoingc2s(0.0);
                }
                if(obj.getScoingc1s()==null){
                    obj.setScoingc1s(0.0);
                }
                if(obj.getScoingc3s()==null){
                    obj.setScoingc3s(0.0);
                }
                if(obj.getScoingc4s()==null){
                    obj.setScoingc4s(0.0);
                }
                if(obj.getScoingc5s()==null){
                    obj.setScoingc5s(0.0);
                }
            //c3
                if(obj.getScoingc1ls()==null){
                    obj.setScoingc1ls(0.0);
                }
                if(obj.getScoingc2ls()==null){
                    obj.setScoingc2ls(0.0);
                }
                if(obj.getScoingc3ls()==null){
                    obj.setScoingc3ls(0.0);
                }
                if(obj.getScoingc4ls()==null){
                    obj.setScoingc4ls(0.0);
                }
                if(obj.getScoingc5ls()==null){
                    obj.setScoingc5ls(0.0);
                }
            //A
                if(obj.getScore1()==null){
                    obj.setScore1(0.0);
                }
                if(obj.getScore2()==null){
                    obj.setScore2(0.0);
                }
                if(obj.getScore3()==null){
                    obj.setScore3(0.0);
                }
                if(obj.getScore4()==null){
                    obj.setScore4(0.0);
                }
                if(obj.getScore5()==null){
                    obj.setScore5(0.0);
                }

                if(obj.getScore1s()==null){
                    obj.setScore1s(0.0);
                }
                if(obj.getScore2s()==null){
                    obj.setScore2s(0.0);
                }
                if(obj.getScore3s()==null){
                    obj.setScore3s(0.0);
                }
                if(obj.getScore4s()==null){
                    obj.setScore4s(0.0);
                }
                if(obj.getScore5s()==null){
                    obj.setScore5s(0.0);
                }


                //B赛场个人统计
                double jg = obj.getScore1() + obj.getScore2() +obj.getScore3();
                jg = (jg / 3);
                BigDecimal bg = new BigDecimal(jg);
                double f1 = bg.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
                obj.setScore1(f1);

                //A赛场个人统计
                double b=(obj.getScoingb1() + obj.getScoingb2() + obj.getScoingb3());
                b = (b / 3);
                b = b + (obj.getTotalScore() * 0.3);
                BigDecimal bp = new BigDecimal(b);
                double bs = bp.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
                obj.setScore2(bs);

                //C赛场个人统计
                Double c=(obj.getScoingc1() + obj.getScoingc2() + obj.getScoingc3());
                c = (c / 3);
                BigDecimal cp = new BigDecimal(c);
                double cs = cp.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
                obj.setScore3(cs);


                Double tatlo=(obj.getScore3()+obj.getScore2()+obj.getScore1());
                BigDecimal bgs = new BigDecimal(tatlo);
                tatlos = bgs.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
                obj.setScore(tatlos);

                list.add(obj);
            }else {
                ExaminationResultsComDO obj = new ExaminationResultsComDO();
                obj.setNum2(stuSignUpComDO.getNum2());
                obj.setName(stuSignUpComDO.getName());
                obj.setUnitName(stuSignUpComDO.getUnitName());
                obj.setIdNumber(stuSignUpComDO.getIdNumber());
                obj.setTicketNumber(stuSignUpComDO.getTicketNumber());
                obj.setComNums(stuSignUpComDO.getComNums());
                obj.setDecrypt(stuSignUpComDO.getDecrypt());
                obj.setScore1(0.0);
                obj.setScore2(0.0);
                obj.setScore3(0.0);
                obj.setScore(0.0);
                list.add(obj);

            }
            //获取最高分
            if(tatlos>=max){
                max = tatlos;
                vtatisticsVo.setMaxScore(max);
            }
            //获取最低分
            if(tatlos <= min){
                min = tatlos;
                vtatisticsVo.setMinScore(min);
            }
        }

        //升序排列
        list = (List)list.stream().sorted(Comparator.comparing(ExaminationResultsComDO::getScore).reversed()).collect(Collectors.toList());
        page.setRecords(list);
        //获取竞赛人数
        vtatisticsVo.setNumberPeople(page.getRecords().size());
        vtatisticsVo.setStuSignUpComDOs(page);
        return Result.ok(vtatisticsVo);
    }

//    /**
//     * A赛场理论成绩统计
//     * @param examinationAnswerSheetComDTO
//     * @return
//     */
//    @ResponseBody
//    @GetMapping("/list")
//    @RequiresPermissions("jzweb:examinationAnswerSheetCom:examinationAnswerSheetComSour")
//    public Result<Page<ExaminationResultsComDO>> list(ExaminationResultsComDO examinationAnswerSheetComDTO){
//
//        // 查询列表数据
//        Page<ExaminationResultsComDO> page = examinationResultsService.selectPage(getPage(ExaminationResultsComDO.class),
//                examinationResultsService.convertToEntityWrapper().orderBy("id", false));
//        List list = new ArrayList();
//        for (int i = 0; i < page.getRecords().size(); i++) {
//            ExaminationResultsComDO obj = (ExaminationResultsComDO) page.getRecords().get(i);
//          StuSignUpComDO stuSignUpComDO=  stuSignUpService.selectById(obj.getStuSignId());
//          if(stuSignUpComDO!=null){
//            obj.setNum2(stuSignUpComDO.getNum2());
//            obj.setName(stuSignUpComDO.getName());
//            obj.setUnitName(stuSignUpComDO.getUnitName());
//            obj.setIdNumber(stuSignUpComDO.getIdNumber());
//            obj.setTicketNumber(stuSignUpComDO.getTicketNumber());
//            Double sour=obj.getTotalScore();
//            if(obj.getScore1()==null){
//                obj.setScore1(0.0);
//            }
//            if(obj.getScore2()==null){
//                obj.setScore2(0.0);
//            }
//            if(obj.getScore3()==null){
//                obj.setScore3(0.0);
//            }
//            Double f=sour+((obj.getScore1()+obj.getScore2()+obj.getScore3())/3);
//            BigDecimal bg = new BigDecimal(f);
//            double f1 = bg.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
//            obj.setScore(f1);
//            list.add(obj);
//          }
//        }
//        page.setRecords(list);
//        return Result.ok(page);
//    }



}
